package com.baizhi.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

/**
 * <p>
 * InnoDB free: 11264 kB 前端控制器
 * </p>
 *
 * @author xiaoxiao
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/cmfz-guru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService cmfzGuruService;
    /**
     * 将数据写入excel表格中
     * @return
     */
    @PostMapping("/upload")
    public Map<String,Object> upload(MultipartFile file) throws Exception {
        //1.通过file对象获取输入流
        InputStream inputStream = file.getInputStream();
        //2.EasyPoi处理流
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);
        //利用工具类将文件内容赋给对象
        List<CmfzGuru> list = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);
        for (CmfzGuru cmfzGuru : list) {
            String guruImg = cmfzGuru.getGuruImage();
            int begin = guruImg.lastIndexOf("\\");
            int last = guruImg.length() - 1;
            cmfzGuru.setGuruImage(guruImg.substring(begin+1, last));
            //保存到数据库
            cmfzGuruService.save(cmfzGuru);
        }
        //返回状态信息
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @GetMapping("selectAllByPage")
    public Map<String,Object> selectAllByPage(Integer page,Integer limit){
        Map<String,Object> map = new HashMap<>();
        PageInfo<CmfzGuru> pi = cmfzGuruService.selectAllByPage(page, limit);
        map.put("code",0);
        map.put("data",pi.getList());
        map.put("count",pi.getTotal());
        return map;
    }

    /**
     * 批量下载
     * @param ids
     * @param
     * @return
     */
    @GetMapping("downloadMany")
    public void downloadMany(int[] ids, HttpServletResponse response) throws IOException {

        //准备数据
        List<CmfzGuru> guruList = new ArrayList<>();
        for (int id : ids) {
            CmfzGuru cmfzGuru = cmfzGuruService.selectById(id);
            guruList.add(cmfzGuru);
        }
        for (CmfzGuru cmfzGuru : guruList) {
            cmfzGuru.setGuruImage("http://localhost:8989/img/guru/" + cmfzGuru.getGuruImage());
        }
        //设置表信息
        ExportParams exportParams = new ExportParams("上师信息", "guru");
        //通过封装的方法写入数据
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,CmfzGuru.class,guruList);
        long millis = System.currentTimeMillis();
        String excelName = "CmfzExcel"+millis+".xlsx";
        // 设置响应的格式和编码
        response.setHeader("Content-Disposition","attachment;fileName=" + URLEncoder.encode(excelName,"utf-8"));
        response.setContentType("application/vnd.ms-excel");
        //响应给浏览器
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

}
